// pages/add-material/add-material.js
var verify = require('../../../utils/verify-token.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    empty:"无",
    array: [
      {  
				id:0,
        name: '小程序补料'
      },
      {
				id:1,
        name: '扫码补料'
      },
      {
				id:2,
        name: '点位补料'
      }
    ],
    array1: [     
      {
				id:1,
        name: '扫码补料'
      },
      {
				id:2,
        name: '点位补料'
      }
    ],
		rangekey:0
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   //console.log("qrId = " + options.qrId)
    var qrId = options.qrId
    var type = options.type
    var token = app.globalData.token
		var baseUrl = app.globalData.baseUrl;
    this.setData({
      type:type,
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth
    })
    var that = this
    wx.request({
      url:baseUrl+ 'feeding/getDevBunkerInfo?id='+qrId+'&type='+type,
      header:{
        'AUTHORIZATION':token
      },
      success:function(res){
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if(res.data.code==200){
          //console.log(res)
          // var unit = res.data.data.bunkerStatusVoList[0].unit;					
          that.setData({
            identifier: res.data.data.identifier,
            deviceShowName: res.data.data.deviceShowName,
            locationName: res.data.data.locationName,
            bunkerStatusVoList: res.data.data.bunkerStatusVoList,    
            deviceId: res.data.data.deviceId,
            // unit: unit
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })

    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      qrId:options.qrId,
    })

     
  },

  reLoad:function(){
    var qrId = this.data.qrId
    var token = app.globalData.token
    var type = this.data.type
    var that = this;
		let baseUrl = app.globalData.baseUrl;
    wx.request({
      url: baseUrl+'feeding/getDevBunkerInfo?id=' + qrId + '&type=' + type,
      header: {
        'AUTHORIZATION': token
      },		
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          //console.log(res)
          //var unit = res.data.data.bunkerStatusVoList[0].unit
          that.setData({
            identifier: res.data.data.identifier,
            deviceShowName: res.data.data.deviceShowName,
            locationName: res.data.data.locationName,
            bunkerStatusVoList: res.data.data.bunkerStatusVoList,  
            deviceId: res.data.data.deviceId,
            // unit: unit
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },
  bindPickerChange: function (e) {
		let that = this;			
		that.setData({
			rangekey:e.detail.value
		})
		let hold = e.currentTarget.dataset.hold;		
		if(hold){
			that.pageTo(Number(e.detail.value)+1,e.currentTarget.dataset.item);
		}else{
			that.pageTo(Number(e.detail.value),e.currentTarget.dataset.item);
		}
  },
	pageTo:function(param,item){
   // console.log(item)
    let bunkerId = item.bunkerId;
		let organId = item.organId;
		let meterialId = item.materialId;
    let meterialName = item.meterialName;	
    let unit = item.unit;	
		let hold = item.hold;
		let deviceId = this.data.deviceId;
		let qrId = "";
		let type = "APP";//小程序补料
		let count = 0;
		let that = this;		
		switch(param){
			case 0:
				wx.navigateTo({
				 url: 'scanCode/scanCode?bunkerId=' + bunkerId + '&organId=' + organId + '&meterialName=' + meterialName + '&hold=' + hold + '&deviceId=' + deviceId + '&meterialId=' + meterialId + '&count=' + count + '&qrId=' + qrId + '&type=' + type
				})
				break;  
			case 1:
				that.scanCode(hold,bunkerId);
				break;
			case 2:
				wx.navigateTo({
          url: 'location-material/location-material?deviceId=' + deviceId + '&bunkerId=' + bunkerId + '&organId=' + organId + '&meterialId=' + meterialId+'&unit='+unit,
				})
				break;				
		}
	},
	//扫码补料
	scanCode:function(hold,bunkerId){
		let qrId = '';
		let type = 'QRCODE';
		let token = app.globalData.token;
		let deviceId = this.data.deviceId;		
		 wx.scanCode({
				success(res){
					//console.log(res.result)
					var length = res.result.length
					for (var i = 0; i < length; i++) {
						if (res.result[length - i - 1] == "=") {
							break;
						}
						qrId = res.result[length - i - 1] + qrId
					}         
//           console.log("qrId = " + qrId)
//           console.log("token = " + token)
					let baseUrl = app.globalData.baseUrl;					
          wx.request({
            url: baseUrl+'feeding/getMeterialInfo?qrId='+qrId,
            header: {
              'AUTHORIZATION': token
            },
            success: function (res) {
              console.log("扫码补料",res)
              if (res.data.code == 60006) {
                verify.clearAll()
              } else if(res.data.code==200){
                var meterialId = res.data.data.meterialId
                var organId = res.data.data.organId
                var curType = res.data.data.organName
                var meterialName = res.data.data.meterialName
                var count = res.data.data.count;  
                wx.navigateTo({
                  url: 'scanCode/scanCode?bunkerId=' + bunkerId + 
                        '&organId=' + organId + 
                        '&meterialName=' + meterialName + 
                        '&hold=' + hold + 
                        '&deviceId=' + deviceId + 
                        '&meterialId=' + meterialId + 
                        '&count=' + count + 
                        '&qrId=' + qrId + 
                        '&type=' + type,
                })
              } else {
                var message = res.data.message
                wx.showModal({
                  title: '',
                  content: message + "！！",
                })
              }
              
            }
          })
        },
        fail(){
          console.log("扫码失败")
        }
      })  
	},


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.reLoad()
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.reLoad();
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1000)
  }


})